package com.work.biz.service.impl;

import java.util.List;

import com.work.common.utils.DateUtils;
import com.work.sys.domain.SysUserRole;
import com.work.sys.mapper.SysUserMapper;
import com.work.sys.mapper.SysUserRoleMapper;
import com.work.sys.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.work.biz.mapper.BizStudentMapper;
import com.work.biz.domain.BizStudent;
import com.work.biz.service.IBizStudentService;

/**
 * 学生Service业务层处理
 *
 * @author lee
 * @date 2021-04-22
 */
@Service
public class BizStudentServiceImpl implements IBizStudentService {
    @Autowired
    private BizStudentMapper bizStudentMapper;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询学生
     *
     * @param studentId 学生ID
     * @return 学生
     */
    @Override
    public BizStudent selectBizStudentById(Long studentId) {
        return bizStudentMapper.selectBizStudentById(studentId);
    }

    /**
     * 查询学生列表
     *
     * @param bizStudent 学生
     * @return 学生
     */
    @Override
    public List<BizStudent> selectBizStudentList(BizStudent bizStudent) {
        bizStudent.setDelFlag("0");
        return bizStudentMapper.selectBizStudentList(bizStudent);
    }

    /**
     * 新增学生
     *
     * @param bizStudent 学生
     * @return 结果
     */
    @Override
    public int insertBizStudent(BizStudent bizStudent) {
        bizStudent.setCreateTime(DateUtils.getNowDate());
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(4L);
        userRole.setUserId(bizStudent.getUserId());
        List<SysUserRole> userRoles = userRoleMapper.selectUserRoleList(userRole);
        if(userRoles.size()<=0) {
            roleService.insertUserRole(userRole);
        }
        return bizStudentMapper.insertBizStudent(bizStudent);
    }

    /**
     * 修改学生
     *
     * @param bizStudent 学生
     * @return 结果
     */
    @Override
    public int updateBizStudent(BizStudent bizStudent) {
        bizStudent.setUpdateTime(DateUtils.getNowDate());
        return bizStudentMapper.updateBizStudent(bizStudent);
    }

    /**
     * 批量删除学生
     *
     * @param studentIds 需要删除的学生ID
     * @return 结果
     */
    @Override
    public int deleteBizStudentByIds(Long[] studentIds) {
        return bizStudentMapper.deleteBizStudentByIds(studentIds);
    }

    /**
     * 删除学生信息
     *
     * @param studentId 学生ID
     * @return 结果
     */
    @Override
    public int deleteBizStudentById(Long studentId) {
        return bizStudentMapper.deleteBizStudentById(studentId);
    }
}
